//
//  ScanCodeViewController.m
//  ScanCode
//
//  Created by YiXuePxh on 15/9/17.
//  Copyright (c) 2015年 上海益学. All rights reserved.
//

#import "ScanCodeViewController.h"
#import "CheckBoxView.h"
#import "WebViewController.h"
@interface ScanCodeViewController ()
@property(nonatomic,strong)AVCaptureDevice* device;
@property(nonatomic,strong)AVCaptureDeviceInput* input;
@property(nonatomic,strong)AVCaptureMetadataOutput* output;
@property(nonatomic,strong)AVCaptureSession* session;
@property(nonatomic,strong)AVCaptureVideoPreviewLayer* preview;
@property(nonatomic,strong)CheckBoxView* chechBox;
@end

@implementation ScanCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self initScanCode];
    //添加查看框
    _chechBox = [[CheckBoxView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:_chechBox];
}
-(void)initScanCode{
    //Device  获取摄像设备
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //Input     创建输入流
    NSError* error = nil;
    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:&error];
    if (error) {
        NSLog(@"input error:%@",[error localizedDescription]);
    }
    //output 创建输出流
    _output = [[AVCaptureMetadataOutput alloc]init];
    //设置代理 在主线程里刷新
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //Session 初始化链接对象
    _session = [[AVCaptureSession alloc]init];
    //高质量采集率
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    //添加 输入流 和 输出流
    if ([_session canAddInput:self.input]) {
        [_session addInput:self.input];
    }
    if ([_session canAddOutput:self.output]) {
        [_session addOutput:self.output];
    }
    //条码类型 AVMetadataObjectTypeQRCode
    _output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode128Code];
    //Preview
    _preview = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    _preview.frame = self.view.bounds;
    [self.view.layer insertSublayer:_preview atIndex:0];
    //Start 开始捕捉
    [_session startRunning];
}
//实现 AVCapatureMetadataOutputObjectDelegate
#pragma mark AVCapatureMetadataOutputObjectDelegate
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    NSString* stringValue;
    if ([metadataObjects count] > 0) {
        //停止扫描
        [_session stopRunning];
        AVMetadataMachineReadableCodeObject* metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
    }
    [_chechBox stopRefresh];
    WebViewController* webVC = [[WebViewController alloc]init];
    webVC.urlString = stringValue;
    [self presentViewController:webVC animated:YES completion:nil];
    NSLog(@"=====扫描结果：%@",stringValue);//将得出的结果展示出来
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
